local wezterm = require("wezterm")

-- In newer versions of wezterm, use the config_builder which will
-- help provide clearer error messages
if wezterm.config_builder then
    config = wezterm.config_builder()
end

wezterm.on("update-right-status", function(window)
    local date = wezterm.strftime("%Y-%m-%d %H:%M:%S   ")
    window:set_right_status(wezterm.format({
        { Text = date },
    }))
end)

-- This table will hold the configuration.
local config = {
    -- color_scheme = "Splurge (terminal.sexy)",
    color_scheme = "Alabaster",
    -- font = wezterm.font("JetBrains Mono"),
    font = wezterm.font("FiraMono Nerd Font"),
    font_size = 15,
    window_padding = {
        top = 0,
        bottom = 0,
        left = 0,
        right = 0,
    },

    window_close_confirmation = "NeverPrompt",
    hide_tab_bar_if_only_one_tab = true,
    enable_scroll_bar = true,

    disable_default_key_bindings = true,
    leader = { key = "a", mods = "ALT" },
    keys = {
        { key = "f", mods = "LEADER", action = wezterm.action.QuickSelect },

        { key = "1", mods = "ALT", action = wezterm.action({ ActivateTab = 0 }) },
        { key = "2", mods = "ALT", action = wezterm.action({ ActivateTab = 1 }) },
        { key = "3", mods = "ALT", action = wezterm.action({ ActivateTab = 2 }) },
        { key = "4", mods = "ALT", action = wezterm.action({ ActivateTab = 3 }) },
        { key = "5", mods = "ALT", action = wezterm.action({ ActivateTab = 4 }) },
        { key = "6", mods = "ALT", action = wezterm.action({ ActivateTab = 5 }) },
        { key = "7", mods = "ALT", action = wezterm.action({ ActivateTab = 6 }) },
        { key = "8", mods = "ALT", action = wezterm.action({ ActivateTab = 7 }) },
        { key = "9", mods = "ALT", action = wezterm.action({ ActivateTab = 8 }) },

        {
            key = "d",
            mods = "SHIFT|CTRL",
            action = wezterm.action({ SplitHorizontal = { domain = "CurrentPaneDomain" } }),
        },
        {
            key = "s",
            mods = "SHIFT|CTRL",
            action = wezterm.action({ SplitVertical = { domain = "CurrentPaneDomain" } }),
        },
        { key = "h", mods = "CTRL", action = wezterm.action({ ActivatePaneDirection = "Left" }) },
        { key = "l", mods = "CTRL", action = wezterm.action({ ActivatePaneDirection = "Right" }) },
        { key = "k", mods = "CTRL", action = wezterm.action({ ActivatePaneDirection = "Up" }) },
        { key = "j", mods = "CTRL", action = wezterm.action({ ActivatePaneDirection = "Down" }) },
        -- { key = "q", mods = "LEADER", action = wezterm.action({ CloseCurrentPane = { confirm = false } }) },
        {
            key = "t",
            mods = "SHIFT|CTRL",
            action = wezterm.action.SpawnTab("CurrentPaneDomain"),
        },
        --     -- misc/useful --
        { key = "F1", mods = "NONE", action = "ActivateCopyMode" },
        { key = "F2", mods = "NONE", action = wezterm.action.ActivateCommandPalette },
        -- { key = "F3", mods = "NONE", action = wezterm.action.ShowLauncher },
        -- { key = "F4", mods = "NONE", action = wezterm.action.ShowTabNavigator },
        -- { key = "F12", mods = "NONE", action = wezterm.action.ShowDebugOverlay },
        -- copy/paste --
        { key = "c", mods = "CTRL|SHIFT", action = wezterm.action.CopyTo("Clipboard") },
        { key = "v", mods = "CTRL|SHIFT", action = wezterm.action.PasteFrom("Clipboard") },

        -- resizes fonts
        {
            key = "f",
            mods = "LEADER",
            action = wezterm.action.ActivateKeyTable({
                name = "resize_font",
                one_shot = false,
                timemout_miliseconds = 1000,
            }),
        },
        -- resizes pane
        {
            key = "p",
            mods = "LEADER",
            action = wezterm.action({
                ActivateKeyTable = {
                    name = "resize_pane",
                    one_shot = false,
                    timeout_milliseconds = 2000,
                    replace_current = false,
                },
            }),
        },
    },

    key_tables = {
        resize_font = {
            { key = "k", action = wezterm.action.IncreaseFontSize },
            { key = "j", action = wezterm.action.DecreaseFontSize },
            { key = "r", action = wezterm.action.ResetFontSize },
            { key = "Escape", action = "PopKeyTable" },
            { key = "q", action = "PopKeyTable" },
        },
        resize_pane = {
            { key = "k", action = wezterm.action.AdjustPaneSize({ "Up", 1 }) },
            { key = "j", action = wezterm.action.AdjustPaneSize({ "Down", 1 }) },
            { key = "h", action = wezterm.action.AdjustPaneSize({ "Left", 1 }) },
            { key = "l", action = wezterm.action.AdjustPaneSize({ "Right", 1 }) },
            { key = "Escape", action = "PopKeyTable" },
            { key = "q", action = "PopKeyTable" },
        },
    },

    mouse_bindings = {
        -- Ctrl-click will open the link under the mouse cursor
        {
            event = { Up = { streak = 1, button = "Left" } },
            mods = "CTRL",
            action = wezterm.action.OpenLinkAtMouseCursor,
        },
    },
}

config.serial_ports = {
    {
        name = "/dev/ttyS0",
        port = "/dev/ttyS0",
        baud = 115200,
    },
}

-- and finally, return the configuration to wezterm
return config
